package com.lanswon.qzsmk.controller.cardManageController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.AccountContants;
import com.lanswon.qzsmk.base.CardContants;
import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.exception.ApplicationException;
import com.lanswon.qzsmk.model.ActiveUser;
import com.lanswon.qzsmk.model.AppKind;
import com.lanswon.qzsmk.model.FeeRules;
import com.lanswon.qzsmk.model.SingleSellCardView;
import com.lanswon.qzsmk.service.appManageService.AppFeeService;
import com.lanswon.qzsmk.service.cardManageService.SellCardService;
import com.lanswon.qzsmk.service.preService.BikeService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.FeeRulesVo;
import com.lanswon.qzsmk.vo.SellCardVo;
import com.lanswon.qzsmk.vo.VoucherVo;

/**
 * 单笔发卡Controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping(value="issueCardSingle")
public class GetCardSingleController {
	
	@Autowired
	SellCardService scs;
	
	@Autowired
	ParamManageService pm;
	
	@Autowired
    AppFeeService afs;
	
	@Autowired
	BikeService bikeService;

	/**
	 * 链接单笔发卡页面
	 * @return
	 */
	@RequiresPermissions("issueCardSingle:index")
	@RequestMapping(value="/getCardSingle",name="单笔发卡,issueCardSingle:index",method={RequestMethod.GET,RequestMethod.POST})
	public String getCardSingle(){
		
		return "GetCardSingle";
	}
	
	
	/**
	 * 查询单笔发卡信息
	 * @param scv
	 * @param pageCount
	 * @return
	 */
	@RequiresPermissions("issueCardSingle:getCardSingleInfo")
	@RequestMapping(value="/getCardSingleInfo",name="单笔发卡查询单笔发卡信息,issueCardSingle:getCardSingleInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<SingleSellCardView> getCardSingleInfo(SellCardVo scv,Integer pageNum,Integer numPerPage){
//		if(scv.getNumPerPage()==null){//加载页面时执行
//			scv.setCurrentPage(1);
//			scv.setNumPerPage(20);
//		}else {
//			if(scv.getCurrentPage()<=1||pageCount==0){
//				scv.setCurrentPage(1);
//			}else if(scv.getCurrentPage()>=pageCount&&pageCount!=0){
//				scv.setCurrentPage(pageCount);
//			}
//		}
		System.out.println(pageNum+","+numPerPage+",");
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		scv.setCurrentPage(pageNum);
		scv.setNumPerPage(numPerPage);
		scv.setStartLine((scv.getCurrentPage()-1)*scv.getNumPerPage());
		DwzPage<SingleSellCardView> dPage=scs.querySingleSellCard(scv);
		return  dPage;
	}
	
	/**
	 * 获取单笔发卡登记页面数据
	 * @param getCardSingle
	 * @param request
	 * @return
	 */
	@RequiresPermissions("issueCardSingle:goGetCardRegister")
	@RequestMapping(value="/goGetCardRegister",name="单笔发卡获取单笔发卡登记页面数据,issueCardSingle:goGetCardRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goGetCardRegisterByCheckBranchNo(String [] getCardSingle,HttpServletRequest request){
		for(String str:getCardSingle){
			System.out.println(str);
		}
		Map<String, Object> map=new HashMap<>();
		ActiveUser activeUser=(ActiveUser) SecurityUtils.getSubject().getSession().getAttribute("activeUser");
		if(!getCardSingle[getCardSingle.length-1].equals("99999999")){
			if(!activeUser.getBranchNo().equals(getCardSingle[getCardSingle.length-1])){
				map.put("statusCode", "300");
	    		map.put("message", "此数据属于"+activeUser.getBranchName()+",无权操作");
	    		return map;
			}
		}
		SellCardVo scv=new SellCardVo();
		scv.setCustomerName(getCardSingle[0]);
		scv.setPaperTypeName(getCardSingle[1]);
		scv.setPaperType(getCardSingle[6]);
		scv.setPaperNo(getCardSingle[2]);
		if(getCardSingle[3]!=null&&!(getCardSingle[4].equals(CardContants.APPLY_TYPE_FIRST))){
			scv.setOldCitizenCardNo(getCardSingle[3]);
			scv=scs.showBaseInfo(scv);
			
		}else{
			scv.setCardPCodeName(getCardSingle[7]);
		}
		scv.setApplyType(getCardSingle[4]);
		scv.setRemark(getCardSingle[5]);
		request.getSession().setAttribute("scv", scv);
		//工本费
		FeeRulesVo vo=new FeeRulesVo();
		vo.setCardpName(getCardSingle[7]);
		vo.setFeeType(AccountContants.FEE_TYPE_NOMINAL_FEE);//工本费代码
		FeeRules feeRules=afs.queryFeeRulesByCardPCodeAndFeeType(vo);
		request.getSession().setAttribute("feeRules", feeRules);
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "issueCardSingle/getCardRegister");//post请求在baseQueryController
		return map;
	}
	

	
	/**
	 * 单笔发卡登记 页面获取session里的内容
	 * @param request
	 * @return
	 */
	@RequiresPermissions("issueCardSingle:getCardRegisterInfo")
	@RequestMapping(value="/getCardRegisterInfo",name="单笔发卡登记 页面获取session里的内容,issueCardSingle:getCardRegisterInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getCardRegisterInfo(HttpServletRequest request){
		Map<String, Object> map=new HashMap<>();
		SellCardVo scv=(SellCardVo) request.getSession().getAttribute("scv");
		List<AppKind> apps=pm.queryAppKind();
		map.put("fee", new BigDecimal(0));
		FeeRules feeRules=(FeeRules) request.getSession().getAttribute("feeRules");
		if(feeRules!=null){
			map.put("fee",feeRules.getFeeBln());
		}
		map.put("scv",scv );
		map.put("apps", apps);
		request.getSession().removeAttribute("feeRules");
		request.getSession().removeAttribute("scv");
		return map;
	}
	
	/**
	 * 单笔发卡提交
	 * @param scv
	 * @return
	 * @throws ApplicationException 
	 */
	@RequiresPermissions("issueCardSingle:commitGetCardRegister")
	@RequestMapping(value="/commitGetCardRegister",name="单笔发卡提交,issueCardSingle:commitGetCardRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitGetCardRegister(SellCardVo scv,HttpServletRequest request) throws ApplicationException{
		Map<String,Object> map=new HashMap<>();
		VoucherVo vv=scs.commitSingleSellCardInfo(scv);
		//map.put("statusCode", "200");
		//map.put("callbackType", "forward");
		//map.put("forwardUrl", "issueCardSingle/getCardSingle");//post请求
		if(scv.getApplyType().equals("1")){
			request.getSession().setAttribute("vo", vv);
			request.getSession().setAttribute("type", "applyRelease");
	        map.put("title", "发卡凭证");
	        map.put("rel", "getCard");
	        //20181206 hgl 增加以下3行
	        map.put("jumpToTabid", "getCardSingle");
			map.put("jumpToUrl", "issueCardSingle/getCardSingle");
			map.put("jumpToTile", "单笔发卡");
		}else if(scv.getApplyType().equals("4")){
			request.getSession().setAttribute("vo", vv);
			request.getSession().setAttribute("type", "changeAndRelease");
	        map.put("title", "换卡凭证");
	        map.put("rel", "changeCard");
	        //20181206 hgl 增加以下3行
	        map.put("jumpToTile", "换卡");
	        map.put("jumpToTabid", "exchangeCard");
	        map.put("jumpToUrl", "exchangeCard/goExchangeCard");
		}else{
			request.getSession().setAttribute("vo", vv);
			request.getSession().setAttribute("type", "supplierAndRelease");
	        map.put("title", "补卡凭证");
	        map.put("rel", "supplierCard");
	        //20181206 hgl 增加以下3行
	        map.put("jumpToTile", "补卡");
	        map.put("jumpToTabid", "reissueCard");
	        map.put("jumpToUrl", "reissueCard/goReissueCard");
		}
		//20181206 hgl 增加以下1行
		map.put("max", "true");
		map.put("statusCode", "200");
        map.put("url", "printCredentials/toHairpinPdfView");
		return map;
		
	}
	@RequiresPermissions("issueCardSingle:getExchangeNewCardInfo")
	@ResponseBody
	@RequestMapping(value="/getExchangeNewCardInfo",name="获取补换卡写卡数据,issueCardSingle:getExchangeNewCardInfo",method=RequestMethod.POST)
	public Map<String, Object> getExchangeNewCardInfo(String oldCitizenCardNo,String newCitizenCardNo,String newCardsn,HttpServletRequest request) throws ApplicationException{
		Map<String, Object>map=new HashMap<>();
		map=bikeService.SupplementAndChangeCard(oldCitizenCardNo, newCitizenCardNo, newCardsn);
		HttpSession session=request.getSession();
		session.setAttribute("oldvipno", map.get("oldvipno"));
		session.setAttribute("oldvipcardsn", map.get("oldvipcardsn"));
		session.setAttribute("newvipno", map.get("newvipno"));
		session.setAttribute("newvipcardsn", map.get("newvipcardsn"));
		session.setAttribute("newCardsn", newCardsn);
		session.setAttribute("newCitizenCardNo", newCitizenCardNo);
		map.put("statusCode", "200");
		return map;
	}
	  
}
